//
//  ExamCateListView.m
//  JPWords
//
//  Created by 梁淑婷 on 2024/6/1.
//

#import "ExamCateListView.h"
#import "ExamCateListCell.h"

@interface ExamCateListView()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)UIView *topBgView;
@property(nonatomic,strong)UILabel *cateTitleLabel;

@end

@implementation ExamCateListView

-(instancetype)initWithFrame:(CGRect)frame andIndex:(NSInteger)index{
    self = [super initWithFrame:frame];
    if(self){
        _sel_index = index;
        self.currentAvailableWidth = frame.size.width;
        [self createUI];
    }
    return self;
}

-(void)refresh{
    CGRect rect = CGRectMake(0,8,self.currentAvailableWidth,self.height-8);
    if(self.grammar&&![self.grammar isEqualToString:@""]){
        self.topBgView.hidden = NO;
        self.cateTitleLabel.text = self.grammar;
        rect = CGRectMake(0,16+54+12,self.currentAvailableWidth,self.height-54-16-12);
    }else{
        self.topBgView.hidden = YES;
    }
    self.tableView.frame = rect;
    [self.tableView reloadData];
}

-(void)refreshWidth{
    self.topBgView.frame = CGRectMake(16,16,self.currentAvailableWidth-32,54);
    [self refresh];
}

-(void)createUI{
    
    __weak typeof(self)self_weak_ = self;
    self.backgroundColor = KbgColor;
    
    self.topBgView = [[UIView alloc]initWithFrame:CGRectMake(16,16,self.currentAvailableWidth-32,54)];
    self.topBgView.backgroundColor = [UIColor whiteColor];
    self.topBgView.layer.cornerRadius = 10;
    self.topBgView.layer.masksToBounds = YES;
    [self addSubview:self.topBgView];
    self.topBgView.hidden = YES;
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithActionBlock:^(id  _Nonnull sender) {
        if(self_weak_.topClickBlock){
            self_weak_.topClickBlock();
        }
    }];
    [self.topBgView addGestureRecognizer:tap];
    
    self.cateTitleLabel = [[UILabel alloc]init];
    self.cateTitleLabel.font = JPUIFontInterMedium(17);
    self.cateTitleLabel.textColor = KFontBlackColor;
    self.cateTitleLabel.userInteractionEnabled = YES;
    [self.topBgView addSubview:self.cateTitleLabel];
    [self.cateTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(@12);
        make.height.mas_equalTo(@22);
        make.centerY.mas_equalTo(self_weak_.topBgView);
        make.right.mas_equalTo(@-12);
    }];
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0,8,self.currentAvailableWidth,self.height-8) style:UITableViewStyleGrouped];
    self.tableView.backgroundColor = KbgColor;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView registerClass:NSClassFromString(@"ExamCateListCell") forCellReuseIdentifier:@"ExamCateListCell"];
    [self addSubview:self.tableView];
    
  
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ExamCateListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ExamCateListCell"];
    if(indexPath.section < self.dataArr.count){
        ExamPaperYearModel *yearModel = self.dataArr[indexPath.section];
        if(indexPath.row < yearModel.list.count){
            ExamPaperYearListModel *model = yearModel.list[indexPath.row];
            cell.model = model;
        }
    }
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 56+12;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArr.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if(section < self.dataArr.count){
        ExamPaperYearModel *yearModel = self.dataArr[section];
        return yearModel.list.count;
    }
    return 0;
}

-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0,0, self.currentAvailableWidth, 48)];
    headerView.backgroundColor = KbgColor;
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(16,18, self.currentAvailableWidth-32,24)];
    titleLabel.font  = JPUIFontInterMedium(17);
    titleLabel.textColor = KFontBlackColor;
    ExamPaperYearModel *yearmodel =  self.dataArr[section];
    titleLabel.text = [NSString stringWithFormat:@"%@年",yearmodel.year];
    [headerView addSubview:titleLabel];
    return headerView;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 48;
}

-(UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *footerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.currentAvailableWidth, 0.00001)];
    footerView.backgroundColor = KbgColor;
    return footerView;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.00001;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.section < self.dataArr.count){
        ExamPaperYearModel *yearModel = self.dataArr[indexPath.section];
        if(indexPath.row < yearModel.list.count){
            ExamPaperYearListModel *model = yearModel.list[indexPath.row];
            if(_listBlock){
                _listBlock(model);
            }
        }
    }
}

@end
